package com.xmy.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xmy.study.constants.MqConstants;
import com.xmy.study.entity.*;
import com.xmy.study.mapper.StudentAccountMapper;
import com.xmy.study.respons.ChaStudyPlan;
import com.xmy.study.respons.ResStudentAccount;
import com.xmy.study.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.study.util.IdWorker;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 * 帐号 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2023-09-08
 */
@Service
public class StudentAccountServiceImpl extends ServiceImpl<StudentAccountMapper, StudentAccount> implements IStudentAccountService {


    @Autowired
    IStudentClassTypeService iStudentClassTypeService;

    @Autowired
    IdWorker idWorker;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    ITaskService iTaskService;

    @Autowired
    IMqPushService iMqPushService;

    @Value("${subjectsId}")
    String SUBJECTS_IDS;


    @Autowired
    IChaPlanService iChaPlanService;

    /**
     * 添加帐号
     *
     * @param perStudentAccountParam
     * @return
     */
    @Override
    public boolean addAccount(ResStudentAccount perStudentAccountParam) {
        try {
            StudentAccount perStudentAccount = new StudentAccount();
            BeanUtils.copyProperties(perStudentAccountParam, perStudentAccount);
            QueryWrapper<StudentAccount> perStudentAccountQueryWrapper = new QueryWrapper<>();
            perStudentAccountQueryWrapper.eq("student_id", perStudentAccount.getStudentId());
            perStudentAccountQueryWrapper.last("limit 1");
            StudentAccount studentAccountInfo = this.getOne(perStudentAccountQueryWrapper);
            if (studentAccountInfo == null) {
                this.save(perStudentAccount);
            } else {
                studentAccountInfo.setClassTypeId(perStudentAccount.getClassTypeId());
                studentAccountInfo.setGradeId(perStudentAccount.getGradeId());
                studentAccountInfo.setYearPart(perStudentAccount.getYearPart());
                studentAccountInfo.setQuarterNum(perStudentAccount.getQuarterNum());
                studentAccountInfo.setYearClassId(perStudentAccount.getYearClassId());
                this.updateById(studentAccountInfo);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 更新帐号信息
     *
     * @param perStudentAccountParam
     * @return
     */
    @Override
    public boolean updateAccount(ResStudentAccount perStudentAccountParam) {
        try {
            //StudentAccount perStudentAccount = new StudentAccount();
            //BeanUtils.copyProperties(perStudentAccountParam, perStudentAccount);
            QueryWrapper<StudentAccount> perStudentAccountQueryWrapper = new QueryWrapper<>();
            perStudentAccountQueryWrapper.eq("student_id", perStudentAccountParam.getStudentId());
            perStudentAccountQueryWrapper.last("limit 1");
            StudentAccount studentAccountInfo = this.getOne(perStudentAccountQueryWrapper);
            if (studentAccountInfo == null) {
                studentAccountInfo = new StudentAccount();
                BeanUtils.copyProperties(perStudentAccountParam, studentAccountInfo);
                this.save(studentAccountInfo);
            } else {
                if (perStudentAccountParam.getSubjectsId() != null && perStudentAccountParam.getSubjectsId().equals(2L)) {
                    if (studentAccountInfo.getYearPart().equals(perStudentAccountParam.getYearPart()) && studentAccountInfo.getQuarterNum().equals(perStudentAccountParam.getQuarterNum())) {
                        //已经被手动调动为基础班的，精品课调整，数学班型不变
                        if (!studentAccountInfo.getClassTypeId().equals(1565023758494501025L)) {
                            studentAccountInfo.setClassTypeId(perStudentAccountParam.getClassTypeId());
                        }
                        //studentAccountInfo.setClassTypeId(perStudentAccountParam.getClassTypeId());
                    } else {
                        studentAccountInfo.setClassTypeId(perStudentAccountParam.getClassTypeId());
                    }
                    studentAccountInfo.setYearPart(perStudentAccountParam.getYearPart());
                    studentAccountInfo.setQuarterNum(perStudentAccountParam.getQuarterNum());
                    studentAccountInfo.setSchoolId(perStudentAccountParam.getSchoolId());
                    studentAccountInfo.setStudentName(perStudentAccountParam.getStudentName());
                    studentAccountInfo.setGradeId(perStudentAccountParam.getGradeId());
                    studentAccountInfo.setYearClassId(perStudentAccountParam.getYearClassId());
                    this.updateById(studentAccountInfo);

                    //培优转提高班
                    if (studentAccountInfo.getClassTypeId().equals(1565023758494502914L) && perStudentAccountParam.getClassTypeId().equals(1575859909042028546L)){
                        UpdateWrapper<ChaPlan> chaPlanUpdateWrapper = new UpdateWrapper<>();
                        chaPlanUpdateWrapper.eq("year_part",perStudentAccountParam.getYearPart());
                        chaPlanUpdateWrapper.eq("quarter_num",perStudentAccountParam.getQuarterNum());
                        chaPlanUpdateWrapper.eq("class_type_id",1565023758494502914L);
                        //chaPlanUpdateWrapper.eq("course_version_id",account.getClassTypeId());
                        chaPlanUpdateWrapper.eq("subjects_id",this.SUBJECTS_IDS);//数学
                        chaPlanUpdateWrapper.eq("student_id",perStudentAccountParam.getStudentId());
                        chaPlanUpdateWrapper.set("class_type_id",1575859909042028546L);
                        iChaPlanService.update(chaPlanUpdateWrapper);
                    }else if (studentAccountInfo.getClassTypeId().equals(1575859909042028546L) && perStudentAccountParam.getClassTypeId().equals(1565023758494501025L)){
                        //提高转基础
                        UpdateWrapper<ChaPlan> chaPlanUpdateWrapper = new UpdateWrapper<>();
                        chaPlanUpdateWrapper.eq("year_part",perStudentAccountParam.getYearPart());
                        chaPlanUpdateWrapper.eq("quarter_num",perStudentAccountParam.getQuarterNum());
                        chaPlanUpdateWrapper.eq("class_type_id",1575859909042028546L);
                        //chaPlanUpdateWrapper.eq("course_version_id",account.getClassTypeId());
                        chaPlanUpdateWrapper.eq("subjects_id",this.SUBJECTS_IDS);//数学
                        chaPlanUpdateWrapper.eq("student_id",perStudentAccountParam.getStudentId());
                        chaPlanUpdateWrapper.set("class_type_id",1565023758494501025L);
                        iChaPlanService.update(chaPlanUpdateWrapper);
                    }

                }
            }
            if (perStudentAccountParam.getSubjectsId() != null && perStudentAccountParam.getSubjectsId().equals(2L)) {
                StudentClassType studentClassType = new StudentClassType();
                BeanUtils.copyProperties(studentAccountInfo, studentClassType);
                studentClassType.setTeacherId(perStudentAccountParam.getTeacherId());
                studentClassType.setGradeSection(perStudentAccountParam.getGradeSection());
                studentClassType.setSubjectsId(perStudentAccountParam.getSubjectsId());
                this.updateClassType(studentClassType);
            }

            PerStudentClassType perStudentClassType = new PerStudentClassType();
            perStudentClassType.setStudentId(studentAccountInfo.getStudentId());
            perStudentClassType.setClassTypeId(studentAccountInfo.getClassTypeId());
            perStudentClassType.setSubjectsId(Long.parseLong(this.SUBJECTS_IDS));
            iMqPushService.updateStudentGradeClassTypeId(perStudentClassType);

            return true;
        } catch (Exception e) {
            System.out.println("error-----" + e.getMessage());
            return false;
        }
    }


    /**
     * 更新班型
     *
     * @param studentClassTypeParam
     * @return
     */
    @Override
    public boolean updateClassType(StudentClassType studentClassTypeParam) {

        QueryWrapper<StudentClassType> studentClassTypeQueryWrapper = new QueryWrapper<>();
        studentClassTypeQueryWrapper.eq("year_part", studentClassTypeParam.getYearPart());
        studentClassTypeQueryWrapper.eq("quarter_num", studentClassTypeParam.getQuarterNum());
        studentClassTypeQueryWrapper.eq("student_id", studentClassTypeParam.getStudentId());
        studentClassTypeQueryWrapper.last("limit 1");
        StudentClassType studentClassType = iStudentClassTypeService.getOne(studentClassTypeQueryWrapper);
        if (studentClassType == null) {
            studentClassType = new StudentClassType();
            BeanUtils.copyProperties(studentClassTypeParam, studentClassType);
            studentClassType.setId(idWorker.nextId());
            iStudentClassTypeService.save(studentClassType);
        } else {
            studentClassType.setClassTypeId(studentClassTypeParam.getClassTypeId());
            studentClassType.setGradeId(studentClassTypeParam.getGradeId());
            studentClassType.setYearClassId(studentClassTypeParam.getYearClassId());
            studentClassType.setGradeSection(studentClassTypeParam.getGradeSection());
            studentClassType.setTeacherId(studentClassTypeParam.getTeacherId());
            iStudentClassTypeService.updateById(studentClassType);
        }
        return true;
    }



}
